package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.library.flowlayout.FlowLayoutManager;
import com.magic.gre.R;
import com.magic.gre.adapter.SearchAdapter;
import com.magic.gre.adapter.SearchHistoryAdapter;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.db.search.DemoDBManager;
import com.magic.gre.entity.WordsBean;
import com.magic.gre.mvp.contract.SearchContract;
import com.magic.gre.mvp.presenter.SearchPresenterImpl;
import com.noname.lib_base_java.util.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-14 16:31
 * 文件名: SearchActivity
 * 描述:
 */
public class SearchActivity extends BaseMVPActivity<SearchPresenterImpl> implements SearchContract.View,
        SearchHistoryAdapter.OnHistoryItemClickLiatener{

    @BindView(R.id.search_edt)
    EditText searchEdt;
    @BindView(R.id.contentRcy)
    RecyclerView contentRcy;
    @BindView(R.id.historyRcy)
    RecyclerView historyRcy;
    @BindView(R.id.history_ll)
    LinearLayout historyLl;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), SearchActivity.class);
        context.startActivity(intent);
    }

    private SearchHistoryAdapter historyAdapter;
    private List<String> historyList = new ArrayList<>();
    private List<WordsBean> dataList = new ArrayList<>();
    private SearchAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected SearchPresenterImpl creatPresenter() {
        return new SearchPresenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        searchEdt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    String content = String.valueOf(textView.getText());
                    DemoDBManager.getInstance().saveContact(content);
                    closeKeyBord(textView);
                    return true;
                }
                return false;
            }
        });


        searchEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                historyRcy.setVisibility(s.length()==0?View.VISIBLE:View.GONE);
                historyLl.setVisibility(s.length()==0?View.VISIBLE:View.GONE);
                contentRcy.setVisibility(s.length()==0?View.GONE:View.VISIBLE);
                if (s.length()!=0){
                    mPresenter.pSearch(String.valueOf(s));
                }
            }
        });

        adapter = new SearchAdapter(this, dataList);
        contentRcy.setLayoutManager(new LinearLayoutManager(this));
        contentRcy.setAdapter(adapter);

        initHistory();


    }


    @OnClick(value = {R.id.cancel_tv})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_tv:
                finish();
                break;
        }
    }

    private void initHistory() {
        historyList = new ArrayList<>();
        historyAdapter = new SearchHistoryAdapter(this, historyList);
        historyAdapter.setOnHistoryItemClickLiatener(this);
        historyRcy.setLayoutManager(new FlowLayoutManager());
        historyRcy.setAdapter(historyAdapter);

        historyList.addAll(DemoDBManager.getInstance().getContactMap());
        historyAdapter.notifyDataSetChanged();
    }


    @Override
    public void vSearch(List<WordsBean> beanList) {
//        contentRcy.setVisibility(View.VISIBLE);
//        historyLl.setVisibility(View.GONE);
        dataList.clear();
        dataList.addAll(beanList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onHistoryItemClick(String content) {
        searchEdt.setText(content);
        searchEdt.setSelection(content.length());
    }
}
